export abstract class CommandBase {

    onFinished: () => void;     //结束

    abstract execute(): void;   //执行

    abstract cancel(): void;    //取消
}

export class CommandPool {

    private _commandList: CommandBase[] = [];
    private currentCommand: CommandBase = null;

    //命令队列 入栈
    push(command: CommandBase) {
        if (this.currentCommand) {
            this._commandList = [];
            this.currentCommand.cancel();
            this.currentCommand = null;
        }
        this._commandList.push(command);
    }

    //按顺序连续执行命令
    seriesExecuteAllCommand() {
        this.pickAndExecuteFirstCommand();
    }

    //提出并执行第一个命令
    private pickAndExecuteFirstCommand() {
        if (this.currentCommand) {
            return;
        }
        if (this._commandList.length == 0) {
            return;
        }
        const command = this._commandList.shift();
        this.currentCommand = command;
        command.onFinished = () => {    //先把队列第一个命令拿出来
            this.currentCommand = null;
            this.pickAndExecuteFirstCommand();
        }
        command.execute();  //再执行
    }
}